// mod_xslt.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "../hencvik_api.h"
#include "resource.h"
#include "autobuild.h"
#include "zdbg.h"

#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/debugXML.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlIO.h>
#include <libxml/DOCBparser.h>
#include <libxml/xinclude.h>
#include <libxml/catalog.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>

#pragma comment (lib, "c:/_temp/libxslt/lib/libxslt.lib")
#pragma comment (lib, "c:/_temp/libxslt/lib/libxml2.lib")

extern int xmlLoadExtDtdDefaultValue;

plugin_link *link;

HINSTANCE g_hInstance;

hencvik_plugin_info plugin_info =
{
	0,
	"mod_xslt",
	__BUILD__,
	"XSLT proc (build "__BUILD_STR__")",
	"Provide XSL transforms\r\nCompiled at "__DATE__" "__TIME__,
	"pf_ziminski (c) 2009",
	"pfzim@mail.ru",
	"http://www.pfzim.ru/",
	"http://www.pfzim.ru/update.php?app=mod_xslt&ver="__BUILD_STR__,
	{ 0xc3c4da88, 0x5dc7, 0x4218, { 0x91, 0x9e, 0x72, 0xa1, 0xa9, 0xbc, 0x0d, 0x23 } } // {C3C4DA88-5DC7-4218-919E-72A1A9BC0D23}
};

// cmd		- "xslt/proc"
// param1	- (db_list_node *) "module/list"
// param2	- (char *) update_url
// return:	HR_OK
void *xslt_proc(char *cmd, void *param1, void *param2)
{
	const char *params[1];
	xsltStylesheetPtr cur = NULL;
	xmlDocPtr res;
	xmlDocPtr doc;
	FILE *f;

	params[0] = NULL;
	xmlSubstituteEntitiesDefault(1);
	xmlLoadExtDtdDefaultValue = 1;
	cur = xsltParseStylesheetFile((const xmlChar *) param1);
	if(!cur)
	{
		ZTRACE("xslt error 1!!!!!!!!!!!!!!!!!!!!!\n");
	}

	doc = xmlParseFile((char *) param2);
	if(!doc)
	{
		ZTRACE("xslt error 2!!!!!!!!!!!!!!!!!!!!!\n");
	}

	res = xsltApplyStylesheet(cur, doc, params);
	if(!res)
	{
		ZTRACE("xslt error 3!!!!!!!!!!!!!!!!!!!!!\n");
	}

	f = fopen("c:\\temp.xml", "w+");
	if(f)
	{
		xsltSaveResultToFile(f, res, cur);
		fclose(f);
	}

	xsltFreeStylesheet(cur);
	xmlFreeDoc(res);
	xmlFreeDoc(doc);

	xsltCleanupGlobals();
	xmlCleanupParser();

	return HR_OK;
}

extern "C" __declspec(dllexport) hencvik_plugin_info *HencvikPluginInfo(unsigned long build)
{
	return &plugin_info;
}

extern "C" __declspec(dllexport) int Load(plugin_link *plink)
{
	link = plink;

	link->call("service/register", "xslt/proc", xslt_proc);

	return LR_OK;
}

extern "C" __declspec(dllexport) int Unload(void)
{
	link->call("service/unregister", "xslt/proc", NULL);

	return 0;
}

extern "C" BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
	g_hInstance = (HINSTANCE) hModule;

    switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
		case DLL_PROCESS_DETACH:
			break;
    }

    return TRUE;
}

